package com.minimalist.common.feign.fallback.lindi;

import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.vo.tradeapplicationinvoice.LindiTradeApplicationInvoiceQueryVO;
import com.minimalist.common.entity.lindi.vo.tradeapplicationinvoice.LindiTradeApplicationInvoiceVO;
import com.minimalist.common.feign.client.lindi.LindiTradeApplicationInvoiceFeignClient;

import java.util.List;
import com.minimalist.common.feign.config.BaseFallbackFactory;
import com.minimalist.common.response.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

/**
 * 林地交易申请发票 Feign 客户端降级工厂
 * 使用FallbackFactory可以获取到异常信息，便于问题排查
 *
 * @author minimalist
 */
@Component
public class LindiTradeApplicationInvoiceFeignClientFallbackFactory extends BaseFallbackFactory<LindiTradeApplicationInvoiceFeignClient> {
    private static final Logger log = LoggerFactory.getLogger(LindiTradeApplicationInvoiceFeignClientFallbackFactory.class);

    @Override
    public LindiTradeApplicationInvoiceFeignClient create(Throwable cause) {
        return new LindiTradeApplicationInvoiceFeignClient() {
            @Override
            public R<?> addTradeApplicationInvoice(LindiTradeApplicationInvoiceVO tradeApplicationInvoiceVO) {
                logServiceUnavailable("添加交易申请发票", tradeApplicationInvoiceVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "添加交易申请发票失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<?> deleteTradeApplicationInvoiceByTradeApplicationInvoiceId(Long tradeApplicationInvoiceId) {
                logServiceUnavailable("删除交易申请发票", tradeApplicationInvoiceId, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "删除交易申请发票失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<?> updateTradeApplicationInvoiceByTradeApplicationInvoiceId(LindiTradeApplicationInvoiceVO tradeApplicationInvoiceVO) {
                logServiceUnavailable("修改交易申请发票", tradeApplicationInvoiceVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "修改交易申请发票失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<PageResp<LindiTradeApplicationInvoiceVO>> getPageTradeApplicationInvoiceList(LindiTradeApplicationInvoiceQueryVO queryVO) {
                logServiceUnavailable("分页查询交易申请发票列表", queryVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "分页查询交易申请发票列表失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<LindiTradeApplicationInvoiceVO> getTradeApplicationInvoiceByTradeApplicationInvoiceId(Long tradeApplicationInvoiceId) {
                logServiceUnavailable("获取交易申请发票详情", tradeApplicationInvoiceId, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "获取交易申请发票详情失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<List<LindiTradeApplicationInvoiceVO>> getTradeApplicationInvoiceListByTradeApplicationId(
                    Long tradeApplicationId) {
                log.error("根据交易申请ID查询发票列表失败，tradeApplicationId: {}, 异常信息: {}",
                        tradeApplicationId, cause.getMessage());
                return R.fail("根据交易申请ID查询发票列表失败");
            }
        };
    }
}
